﻿/*

Copyright 2009 Guillaume Amringer

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

package {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import org.papervision3d.core.math.Number3D;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.parsers.Collada;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import lt.uza.utils.Global;
	import gs.TweenMax;
	
	public class Unit extends DisplayObject3D{
		
		public var globals:Global = Global.getInstance();
		
		private var _material:BitmapMaterial;
		private var _collada:Collada;
		
		public var owner:Player;
		public var SEXid:uint;
		public var destination:Number3D;
		public var speed:Number = 0;
		
		public var combatMode:Boolean = false;
		
		private const MAX_SPEED:int = 20;
		private const MASS:int = 500;
		
		//	--TEMP--
		public var geom:Cube;
		public var cmaterial:ColorMaterial = new ColorMaterial(0xff0000, 1, true);
		//	--------
		
		public function Unit(pOwner:Player):void {
			
			owner = pOwner;
			
//			_material = new BitmapMaterial();
//			_collada = new Collada();
			
			geom = new Cube(new MaterialsList({all:cmaterial}), 200, 200, 200);
			this.addChild(geom);
			
			geom.addEventListener(InteractiveScene3DEvent.OBJECT_RELEASE, _clickHandler);
		}
		
		public function accelerate():void {
			//new TweenMax(this, 2, { speed:MAX_SPEED } );
			this.speed = MAX_SPEED;
		}
		
		public function enterFrameEvent():void {
			if (destination) {
				var dX:Number = destination.x - this.x;	//	Delta X
				var dZ:Number = destination.z - this.z;	//	Delta Z
				var bH:Number = Math.sqrt(Math.abs(Math.pow(dX,2) + Math.pow(dZ,2)));	//	Big hypothenuse
				
				var aX:Number = dX * speed / bH;	//	X translation
				var aZ:Number = dZ * speed / bH;	//	Z translation
				
				if (speed >= bH) {
					this.x = destination.x;
					this.z = destination.z;
					destination = null;
				} else {
					this.x += aX;
					this.z += aZ;
				}
			}
		}
		
		private function _clickHandler(e:InteractiveScene3DEvent):void {
			globals.game.gameInterface.changetext(SEXid);
			globals.game.unitsManager.setSelection(new Array(SEXid.toString()));
		}
	}
}